package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.bean.CommentBean;
import com.yuejian_06.yuejian_06.bean.FollowListBean;
import com.yuejian_06.yuejian_06.bean.PicBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.service.MeService;
import com.yuejian_06.yuejian_06.service.PicService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/10/13.
 */

public class PicModel {
    private PicService picService;
    private Retrofit retrofit;
    public PicModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void getPicList(final MeListener meListener) {
        picService=retrofit.create(PicService.class);
        Call<List<PicBean>> call=picService.getPicList();
        call.enqueue(new Callback<List<PicBean>>() {
            @Override
            public void onResponse(Call<List<PicBean>> call, Response<List<PicBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<PicBean>> call, Throwable t) {
                meListener.onFail("帖子失败!");
            }
        });
    }

    public void getPicInfo(String pic_id, final MeListener meListener) {
        picService=retrofit.create(PicService.class);
        Call<PicBean> call=picService.getPicInfo(pic_id);
        call.enqueue(new Callback<PicBean>() {
            @Override
            public void onResponse(Call<PicBean> call, Response<PicBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<PicBean> call, Throwable t) {
                meListener.onFail("帖子失败!");
            }
        });
    }

    public void getCommentList(String pic_id, final MeListener meListener) {
        picService=retrofit.create(PicService.class);
        Call<List<CommentBean>> call=picService.getCommentList(pic_id);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                meListener.onFail("留言列表失败!");
            }
        });
    }

    public void getPostComment(String user_id,String pic_id,String comment_content,final MeListener meListener) {
        picService=retrofit.create(PicService.class);
        Call<SuccessBean> call=picService.getPostComment(user_id,pic_id,comment_content);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("留言列表失败!");
            }
        });
    }
    public void getPostPic(String user_id,String pic_main ,final MeListener meListener) {
        picService=retrofit.create(PicService.class);
        Call<SuccessBean> call=picService.getPostPic(user_id,pic_main);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("发表晒图失败!");
            }
        });
    }
}
